package com.ch.forum.service.impl;

import com.ch.forum.bo.ArtVo;
import com.ch.forum.bo.PageResult;
import com.ch.forum.bo.ResponseResult;
import com.ch.forum.dao.ArtHistoryMapper;
import com.ch.forum.dao.ArtZanHistoryMapper;
import com.ch.forum.dao.ArticelMapper;
import com.ch.forum.model.ArtHistoryModel;
import com.ch.forum.model.ArtModel;
import com.ch.forum.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticelMapper articelMapper;

    @Autowired
    private ArtHistoryMapper historyMapper;

    @Autowired
    private ArtZanHistoryMapper artZanHistoryMapper;

    @Override
    public void sendPreArticle(int num) {
        List<Long> ids = articelMapper.findPrdArts();

        // 修改状态
        StringBuilder sb = new StringBuilder();
        if(ids != null && ids.size() > 0){
            if(num <= ids.size()){
                for(int i = 0; i<num; i++){
                    sb.append(ids.get(i)).append(",");
                }
            }

            if(num > ids.size()){
                for(int i = 0; i<ids.size(); i++){
                    sb.append(ids.get(i)).append(",");
                }
            }

            articelMapper.updateIsPre(sb.toString().substring(0, sb.toString().length() -1));
        }
    }

    @Override
    public ResponseResult addArticele(ArtModel artModel) {
        artModel.setDate(new Date(System.currentTimeMillis()));

        articelMapper.addArticel(artModel);

        ArtHistoryModel historyModel = new ArtHistoryModel();
        historyModel.setArtId(artModel.getId());
        historyModel.setDate(artModel.getDate());
        historyModel.setUserId(artModel.getUserId());
        historyMapper.addHistory(historyModel);

        return new ResponseResult(true);
    }

    @Override
    public ResponseResult findArtById(Long id) {
        ArtVo result = articelMapper.findArtById(id);
        int sql = articelMapper.updateClickNum(result.getId(), result.getClickNum() + 1);
        if(sql > 0){
            result.setClickNum(result.getClickNum() + 1);
        }
        return new ResponseResult(true,"result", result);
    }

    @Override
    public ResponseResult findArtList(int pageNo, int pageSize) {

        if(pageNo == 0 && pageSize == 0){
            return new ResponseResult(true, "列表数据", articelMapper.findArtList());
        }

        int no = (pageNo - 1)*pageSize;

        int totalRecords = articelMapper.findArtTotal();

        return new ResponseResult(
                true,
                "result",
                new PageResult(pageNo, pageSize, totalRecords, articelMapper.findArtListPage(no, pageSize)
                )
        );
    }

    @Override
    public ResponseResult findLatestPage(int pageNo, int pageSize) {
        int no = (pageNo - 1)*pageSize;

        int totalRecords = articelMapper.findArtTotal();

        return new ResponseResult(
                true,
                "result",
                new PageResult(pageNo, pageSize, totalRecords, articelMapper.findArtLatestPage(no, pageSize)
                )
        );
    }

    @Override
    public ResponseResult findSearchKeyWordsPage(int pageNo, int pageSize, String key) {
        int no = (pageNo - 1)*pageSize;

        int totalRecords = articelMapper.findSearchTotal("%" + key + "%");

        return new ResponseResult(
                true,
                "result",
                new PageResult(pageNo, pageSize, totalRecords, articelMapper.findSearchKeyWordsPage(no, pageSize, "%" + key + "%")
                )
        );
    }

    @Override
    public ResponseResult findArtListByUserId(Long userId, int pageNo, int pageSize) {
        return new ResponseResult(
                true,
                "result",
                new PageResult(
                        pageNo,
                        pageSize,
                        articelMapper.countArtListByUserId(userId),
                        articelMapper.findArtListByUserId(userId, pageNo, pageSize)
                )
        );
    }

    @Override
    public ResponseResult addZanNum(Long artId, Long userId) {
        ArtVo vo = articelMapper.findArtById(artId);
        if(vo == null){
            return new ResponseResult(false, "请先确认文章ID信息");
        }

        Long isZan = artZanHistoryMapper.findHisIsExist(artId, userId);
        if(isZan > 0){
            return new ResponseResult(false, "请勿重复赞");
        }

        Long isZan2 = artZanHistoryMapper.findHisIsExist2(artId, userId);
        if(isZan2 > 0){
            artZanHistoryMapper.updateIsZan(1, artId, userId);
            articelMapper.updateZanNum(artId, vo.getZanNum() == null ? 1 : vo.getZanNum() + 1);
            return new ResponseResult(true, "赞赞赞！");
        }

        artZanHistoryMapper.add(artId, userId, 1);
        articelMapper.updateZanNum(artId, vo.getZanNum() == null ? 1 : vo.getZanNum() + 1);
        return new ResponseResult(true, "赞赞赞！");
    }

    @Override
    public ResponseResult zanCancel(Long artId, Long userId) {
        ArtVo vo = articelMapper.findArtById(artId);
        if(vo == null){
            return new ResponseResult(false, "请先确认文章ID信息");
        }
        artZanHistoryMapper.updateIsZan(0, artId, userId);
        articelMapper.updateZanNum(artId, vo.getZanNum() == null ? 1 : vo.getZanNum() - 1);
        return new ResponseResult(true, "取消赞");
    }

    public void sendArticle(String ids){
        // type time
    }
}
